package models

import (
	"database/sql"
	dao "sport_booking/dao"
	"sport_booking/utils/message"
)

type Activity struct {
	ID          int64  `json:"activity_id,omitempty" db:"activity_id"`
	Name        string `json:"activity_name,omitempty" db:"activity_name" validate:"required" label:"活动名称"`
	Note        string `json:"activity_note,omitempty" db:"activity_note" validate:"required" label:"活动小提示"`
	Address     string `json:"activity_address,omitempty" db:"activity_address" validate:"required" label:"活动地址"`
	CreateStamp int64  `json:"activity_create_stamp,omitempty" db:"activity_create_stamp"`
	UpdateStamp int64  `json:"activity_update_stamp,omitempty" db:"activity_update_stamp"`
	StartStamp  int64  `json:"activity_start_stamp,omitempty" db:"activity_start_stamp" validate:"required" label:"活动开始时间"`
	EndStamp    int64  `json:"activity_end_stamp,omitempty" db:"activity_end_stamp" validate:"required" label:"活动结束时间"`
	User
}

/*
添加活动信息
*/
func AddActivityInfo(activity *Activity) (int64, int) {
	sqlStr := "INSERT INTO bk_activitys (activity_name,activity_note,activity_address,activity_create_stamp," +
		"activity_update_stamp,activity_start_stamp,activity_end_stamp,user_uuid) VALUES (?,?,?,?,?,?,?,?)"
	result, err := dao.DB.Exec(sqlStr, activity.Name, activity.Note, activity.Address, activity.CreateStamp,
		activity.UpdateStamp, activity.StartStamp, activity.EndStamp, activity.User.UUID)
	if err != nil {
		return 0, message.ErrorAddActivity
	}
	id, err := result.LastInsertId()
	if err != nil {
		return 0, message.ErrorGetLastInsertId
	}
	if id <= 0 {
		return 0, message.ERROR
	}
	return id, message.SUCCESS
}

/*
根据时间戳活动信息检查活动是否存在
*/
func CheckActivityCount(activity *Activity) int {
	var count int
	sqlStr := "SELECT COUNT(activity_id) FROM bk_activitys WHERE activity_name=? AND activity_address=? " +
		"AND activity_start_stamp=? AND activity_end_stamp=?"
	err := dao.DB.Get(&count, sqlStr, activity.Name, activity.Address, activity.StartStamp, activity.EndStamp)
	if err != nil {
		return message.ErrorCheckActivity
	}
	if count == 0 {
		return message.SuccessActivityNotExist
	}
	return message.SuccessActivityExist
}

/*
根据uuid和活动id修改活动信息
*/
func UpdateActivityInfo(activity *Activity) int {
	sqlStr := "UPDATE bk_activitys SET activity_name=?,activity_note=?,activity_address=?,activity_update_stamp=?," +
		"activity_start_stamp=?,activity_end_stamp=? WHERE user_uuid=? AND activity_id=?"
	result, err := dao.DB.Exec(sqlStr, activity.Name, activity.Note, activity.Address, activity.UpdateStamp,
		activity.StartStamp, activity.EndStamp, activity.User.UUID, activity.ID)
	if err != nil {
		return message.ErrorUpdateActivity
	}
	row, err := result.RowsAffected()
	if err != nil {
		return message.ErrorGetRowsAffected
	}
	if row == 0 {
		return message.ERROR
	}
	return message.SUCCESS
}

/*
根据uuid查看活动信息列表区间
page 第几页
line 多少行
*/
func GetActivityInfoListByUUID(uuid string, next *Next) ([]Activity, int) {
	var activity []Activity
	sqlStr := "SELECT activity_id,activity_name,activity_note,activity_address,activity_create_stamp,activity_start_stamp," +
		"activity_end_stamp FROM bk_activitys WHERE user_uuid=? ORDER BY activity_create_stamp LIMIT ?,?"
	err := dao.DB.Select(&activity, sqlStr, uuid, (next.Page-1)*next.Line, next.Line)
	if err != nil {
		return nil, message.ErrorGetActivityList
	}
	if activity == nil {
		return nil, message.ErrorActivityDataNil
	}
	return activity, message.SUCCESS
}

/*
查询活动信息列表区间
page 第几页
line 多少行
*/
func GetActivityInfoList(next *Next) ([]Activity, int) {
	var activity []Activity
	sqlStr := "SELECT activity_id,activity_name,activity_note,activity_address,activity_create_stamp,activity_start_stamp," +
		"activity_end_stamp FROM bk_activitys ORDER BY activity_create_stamp LIMIT ?,?"
	err := dao.DB.Select(&activity, sqlStr, (next.Page-1)*next.Line, next.Line)
	if err != nil {
		return nil, message.ErrorGetActivityList
	}
	if activity == nil {
		return nil, message.ErrorActivityDataNil
	}
	return activity, message.SUCCESS
}

/*
获取最新活动信息
*/
func GetNewActivityInfo() (Activity, int) {
	var activity Activity
	sqlStr := "SELECT activity_id,activity_name,activity_note,activity_address,activity_create_stamp,activity_update_stamp," +
		"activity_start_stamp,activity_end_stamp " +
		"FROM bk_activitys " +
		"WHERE activity_id=(SELECT MAX(activity_id) FROM bk_activitys) "
	err := dao.DB.Get(&activity, sqlStr)
	if err == sql.ErrNoRows {
		return activity, message.ErrorGetNewActivityNotExist
	}
	if err != nil {
		return activity, message.ErrorGetNewActivity
	}
	return activity, message.SUCCESS
}

/*
检查活动ID是否存在
*/
func CheckActivityIdState(activityId int64) int {
	var count int
	sqlStr := "SELECT COUNT(activity_id) FROM bk_activitys WHERE activity_id=?"
	err := dao.DB.Get(&count, sqlStr, activityId)
	if err != nil {
		return message.ErrorCheckActivityId
	}
	if count == 0 {
		return message.SuccessActivityIdNotExist
	}
	return message.SuccessActivityIdExist
}

///*
//根据uuid查询所有活动列表区间
//page 第几页
//line 多少行
//*/
//func GetActivityInfoAllByUUID(uuid string, next *Next) ([]Activity, int) {
//	var activity []Activity
//	sqlStr := "SELECT ba.activity_id,ba.activity_name,ba.activity_note,ba.activity_address,ba.activity_create_stamp," +
//		"ba.activity_start_stamp,ba.activity_end_stamp,bu.user_name,bu.user_email,bu.user_department,bu.user_job " +
//		"FROM bk_activitys AS ba,bk_users AS bu " +
//		"WHERE ba.user_uuid=bu.user_uuid " +
//		"AND ba.user_uuid=? " +
//		"ORDER BY ba.activity_create_stamp " +
//		"LIMIT ?,? "
//	err := dao.DB.Select(&activity, sqlStr, uuid, (next.Page-1)*next.Line, next.Line)
//	if err != nil {
//		return activity, message.ERROR
//	}
//	return activity, message.SUCCESS
//}
//
///*
//查询所有活动列表区间
//*/
//func GetActivityInfoAll(next *Next) ([]Activity, int) {
//	var activity []Activity
//	sqlStr := "SELECT ba.activity_id,ba.activity_name,ba.activity_note,ba.activity_address,ba.activity_create_stamp," +
//		"ba.activity_start_stamp,ba.activity_end_stamp,bu.user_name,bu.user_email,bu.user_department,bu.user_job " +
//		"FROM bk_activitys AS ba,bk_users AS bu " +
//		"WHERE ba.user_uuid=bu.user_uuid " +
//		"ORDER BY ba.activity_create_stamp " +
//		"LIMIT ?,? "
//	err := dao.DB.Select(&activity, sqlStr, (next.Page-1)*next.Line, next.Line)
//	if err != nil {
//		return activity, message.ERROR
//	}
//	return activity, message.SUCCESS
//}
